<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var nums = function (n, k) {
      let res = [];
      function dfs(str, num) {
        // 匹配到终止条件，终止
        if (str.length === n) {
          str = parseInt(str);
          if (res.indexOf(str) === -1) {
            res.push(str);
          }
          return;
        }
        doNext(str, num + k);
        doNext(str, num - k);
      }

      function doNext(str, num) {
        if (num >= 0 && num < 10) {
          dfs(str + num, num);
        }
      }
      // 首字母从1开始，到9结束
      for (let i = 1; i < 10; i++) {
        dfs(i + '', i);
      }
      return res;
    };
    console.log(nums(n = 2, k = 1))
  </script>
</body>

</html>